#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./paas/esb/apps/manager/system/views.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
from django.shortcuts import render
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.views.generic import View
from esb.bkcore.models import ComponentSystem
from esb.common.decorators import is_user_super
from .forms import ComponentSystemForm, EditComponentSystemForm
from esb.configs.default import menu_items
from ..utils import md2html
menu_active_item = 'system_manager'

class SystemListView(View):
    """System list page"""

    @is_user_super
    def get(self, request):
        systems = ComponentSystem.objects.all().order_by('name')
        return render(request, 'manager/system/list.html', {'systems': systems,
           'menu_items': menu_items,
           'menu_active_item': menu_active_item,
           'system_term_html': md2html('system')
           })


class AddSystemView(View):
    """Add ComponentSystem view"""

    @is_user_super
    def get(self, request):
        form = ComponentSystemForm()
        return render(request, 'manager/system/add.html', {'form': form,
           'menu_items': menu_items,
           'menu_active_item': menu_active_item
           })

    @is_user_super
    def post(self, request):
        form = ComponentSystemForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('manager.system.list'))
        return render(request, 'manager/system/add.html', {'form': form,
           'menu_items': menu_items,
           'menu_active_item': menu_active_item
           })


class EditSystemView(View):
    """Edit system view"""

    @is_user_super
    def get(self, request, system_id):
        system = ComponentSystem.objects.get(id=system_id)
        form = EditComponentSystemForm(instance=system)
        return render(request, 'manager/system/edit.html', {'form': form,
           'menu_items': menu_items,
           'menu_active_item': menu_active_item
           })

    @is_user_super
    def post(self, request, system_id):
        system = ComponentSystem.objects.get(id=system_id)
        form = EditComponentSystemForm(request.POST, instance=system)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('manager.system.list'))
        return render(request, 'manager/system/edit.html', {'form': form,
           'menu_items': menu_items,
           'menu_active_item': menu_active_item
           })
